package com.example.ams.controller;

import com.example.ams.dto.EmployeeDTO;
import com.example.ams.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理控制器
 * Employee Controller
 */
@RestController
@RequestMapping("/api/employees")
@CrossOrigin(origins = "*")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 查询所有员工
     * GET /api/employees
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllEmployees(
            @RequestParam(required = false) String dept,
            @RequestParam(required = false) Integer status) {
        
        List<EmployeeDTO> employees;
        
        if (dept != null && !dept.isEmpty()) {
            employees = employeeService.findByDept(dept);
        } else if (status != null) {
            employees = employeeService.findByStatus(status);
        } else {
            employees = employeeService.findAll();
        }

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "success");
        response.put("data", employees);
        response.put("total", employees.size());

        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID查询员工
     * GET /api/employees/{id}
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getEmployeeById(@PathVariable Long id) {
        try {
            EmployeeDTO employee = employeeService.findById(id);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "success");
            response.put("data", employee);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 404);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 新增员工
     * POST /api/employees
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> createEmployee(
            @Validated @RequestBody EmployeeDTO employeeDTO) {
        try {
            EmployeeDTO created = employeeService.create(employeeDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 201);
            response.put("message", "员工创建成功");
            response.put("data", created);
            return ResponseEntity.status(HttpStatus.CREATED).body(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }

    /**
     * 更新员工
     * PUT /api/employees/{id}
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateEmployee(
            @PathVariable Long id,
            @Validated @RequestBody EmployeeDTO employeeDTO) {
        try {
            EmployeeDTO updated = employeeService.update(id, employeeDTO);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "员工更新成功");
            response.put("data", updated);
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }

    /**
     * 删除员工
     * DELETE /api/employees/{id}
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteEmployee(@PathVariable Long id) {
        try {
            employeeService.delete(id);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "员工删除成功");
            return ResponseEntity.ok(response);
        } catch (RuntimeException e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 400);
            response.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(response);
        }
    }
}

